<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "post".
 *
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $tags
 * @property integer $status
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $author_id
 */
class Post extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'post';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','content','status','author_id'],'required'],
            [['content', 'tags'], 'string'],
            [['status', 'create_time', 'update_time', 'author_id'], 'integer'],
            [['title'], 'string', 'max' => 128],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'content' => '内容',
            'tags' => '标签',
            'status' => '状态',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'author_id' => '作者',
        ];
    }
    
    /**
     * [getStatus0 关联文章状态表---多对一]
     * @return [type] [description]
     */
    public function getStatus0()    //0避免和status属性名冲突
    {
        //hasOne多对一,第一个参数是关联表的模型类名,第二个参数是关联的字段名
        return $this->hasOne(Poststatus::className(),['id'=>'status']);
    }
    
    /**
     * [getComments 关联评论表---一对多]
     * @return [type] [description]
     */
    public function getComments()
    {
        //hasMany一对多,第一个参数是关联表的模型类名,第二个参数是关联的字段名,外键=>对应post字段名
        return $this->hasMany(Comment::className(),['post_id'=>'id']);
    }
    
    /**
     * [getAuthor 关联管理员用户表???为啥呢]
     * @return [type] [description]
     */
    public function getAuthor()
    {
        return $this->hasOne(Adminuser::className(),['id'=>'author_id']);
    }
}
